﻿using System;
using ProtoBuf;


namespace ProtobufDemo {
	public class Program {

		[ProtoContract]
		public class Address {
			[ProtoMember (1)]
			public string Line1;
			[ProtoMember (2)]
			public string Line2;
		}

		[ProtoContract]
		public class Person {
			[ProtoMember (1)]
			public int Id;
			[ProtoMember (2)]
			public string Name;
			[ProtoMember (3)]
			public Address Addr;
		}


		public static void Main () {
			Person person = new Person ();
			person.Id = 1;
			person.Name = "First";
			person.Addr = new Address { Line1 = "line1", Line2 = "line2" };

			// ProtoBuf序列化
			using (var file = System.IO.File.Create ("Person.bin")) {
				ProtoBuf.Serializer.Serialize (file, person);

			}

			// ProtoBuf反序列化
			Person binPerson = null;
			using (var file = System.IO.File.OpenRead ("Person.bin")) {
				binPerson = ProtoBuf.Serializer.Deserialize<Person> (file);
			}

			System.Console.WriteLine (binPerson.Name);
		}
	}
}

